package com.zl.autocode.service.autocode.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zl.autocode.common.constant.AutocodeEnum;
import com.zl.autocode.dao.autocode.ZlTableColumnMapper;
import com.zl.autocode.dto.autocode.tableColumn.ReqTableColumnAddDTO;
import com.zl.autocode.dto.autocode.tableColumn.ReqTableColumnDelDTO;
import com.zl.autocode.dto.autocode.tableColumn.ReqTableColumnDetailDTO;
import com.zl.autocode.dto.autocode.tableColumn.ReqTableColumnEditDTO;
import com.zl.autocode.dto.autocode.tableColumn.ReqTableColumnQueryDTO;
import com.zl.autocode.dto.autocode.tableColumn.ReqTableColumnStateDTO;
import com.zl.autocode.dto.autocode.tableColumn.ResTableColumnAddDTO;
import com.zl.autocode.dto.autocode.tableColumn.ResTableColumnDelDTO;
import com.zl.autocode.dto.autocode.tableColumn.ResTableColumnDetailDTO;
import com.zl.autocode.dto.autocode.tableColumn.ResTableColumnEditDTO;
import com.zl.autocode.dto.autocode.tableColumn.ResTableColumnQueryDTO;
import com.zl.autocode.dto.autocode.tableColumn.ResTableColumnStateDTO;
import com.zl.autocode.dto.autocode.tableColumn.ResTableColumnVO2;
import com.zl.autocode.entity.autocode.ZlTableColumn;
import com.zl.autocode.entity.autocode.po.ZlTableColumnVo;
import com.zl.autocode.service.autocode.ITableColumnService;
import com.zl.autocode.service.base.AutocodeBaseManager;
import com.zl.common.dto.ReqDTO;
import com.zl.common.dto.pagination.PageReqVO;
import com.zl.common.enums.resp.GlobalRespEnum;
import com.zl.common.exception.BizException;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;

/**
 * 数据库表属性信息
 * @author ZL
 */
@Service("autocodeTableColumnService")
public class TableColumnServiceImpl extends AutocodeBaseManager implements ITableColumnService {

    @Autowired
    private ZlTableColumnMapper tableColumnMapper;

    @Override
    public ResTableColumnQueryDTO queryTableColumnList(ReqDTO<ReqTableColumnQueryDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTableColumnQueryDTO reqData = reqInfo.getReqData();
        PageReqVO page = pageHandle(reqInfo.getPageInfo());//处理分页数据
        ResTableColumnQueryDTO resDTO = BeanUtil.copyProperties(page, ResTableColumnQueryDTO.class);
        if(StringUtils.isNotBlank(reqData.getColumnName())){
            reqData.setColumnName("%"+reqData.getColumnName()+"%");
        }
        if(StringUtils.isNotBlank(reqData.getColumnTitle())){
            reqData.setColumnTitle("%"+reqData.getColumnTitle()+"%");
        }
        if(StringUtils.isNotBlank(reqData.getColumnCode())){
            reqData.setColumnCode("%"+reqData.getColumnCode()+"%");
        }
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData,false,true);
        
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlTableColumnVo> list = tableColumnMapper.selectListPage(paramMap);
        resDTO.setData(BeanUtil.copyToList(list, ResTableColumnVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResTableColumnDetailDTO queryTableColumnDetail(ReqDTO<ReqTableColumnDetailDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTableColumnDetailDTO reqData = reqInfo.getReqData();
        ResTableColumnDetailDTO res = new ResTableColumnDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        ZlTableColumn view = tableColumnMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResTableColumnAddDTO insertTableColumnInfo(ReqDTO<ReqTableColumnAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTableColumnAddDTO reqData = reqInfo.getReqData();
        ResTableColumnAddDTO res = new ResTableColumnAddDTO();
        Date time = getSystemTime().getDate();
        
        ZlTableColumn record = new ZlTableColumn();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setColumnId(IdUtil.simpleUUID());
        record.setColumnCode(record.getColumnCode().toLowerCase());
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("columnId",record.getColumnId());
        //检查主键是否冲突
        if(tableColumnMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalRespEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        tableColumnMapper.insertSelective(record);
        
        ZlTableColumn keyInfo = tableColumnMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResTableColumnEditDTO updateTableColumnInfo(ReqDTO<ReqTableColumnEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTableColumnEditDTO reqData = reqInfo.getReqData();
        ResTableColumnEditDTO res = new ResTableColumnEditDTO();
        Date time = getSystemTime().getDate();
        
        ZlTableColumn record = new ZlTableColumn();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        record.setColumnCode(record.getColumnCode().toLowerCase());
        
        tableColumnMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("columnId",record.getColumnId());
        ZlTableColumn keyInfo = tableColumnMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResTableColumnDelDTO deleteTableColumnInfo(ReqDTO<ReqTableColumnDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTableColumnDelDTO reqData = reqInfo.getReqData();
        ResTableColumnDelDTO res = new ResTableColumnDelDTO();
        Date time = getSystemTime().getDate();
        
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData);
        tableColumnMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResTableColumnStateDTO updateTableColumnState(ReqDTO<ReqTableColumnStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTableColumnStateDTO reqData = reqInfo.getReqData();
        ResTableColumnStateDTO res = new ResTableColumnStateDTO();
        Date time = getSystemTime().getDate();
         
        ZlTableColumn record = new ZlTableColumn();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        tableColumnMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("columnId",record.getColumnId());
        ZlTableColumn keyInfo = tableColumnMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}